<template>
  <el-form class="login-form"
           status-icon
           :rules="loginRules"
           ref="loginForm"
           :model="loginForm"
           label-width="0">
    <el-form-item prop="username">
      <el-input @keyup.enter="handleLogin"
                v-model="loginForm.username"
                auto-complete="off"
                placeholder="用户名">

      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input @keyup.enter="handleLogin"
                type="password"
                show-password
                v-model="loginForm.password"
                auto-complete="off"
                placeholder="密码">

      </el-input>
    </el-form-item>


    <el-form-item>
      <el-button type="primary"
                 @click.prevent="handleLogin"
                 class="login-submit">登 录
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {loginByUsername} from "@/api/web/user.js";
import {loginUrlStore, useTagList, useRouterStore, useUserInfoStore, useTokenStore} from "@/stores/index.js";
import router from "@/router/index.js";
import * as CryptoJS from 'crypto-js'

export default {
  name: "userlogin",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
        code: "",
        redomStr: ""
      },
      loginUrl:'',
      userInfo:{},
      tags:[],
      novaToken:null,
      beforeRoute:'',
      checked: false,
      code: {
        src: "",
        value: "",
        len: 4,
        type: "text"
      },
      loginRules: {
        username: [
          {required: true, message: "请输入用户名", trigger: "blur"}
        ],
        password: [
          {required: true, message: "请输入密码", trigger: "blur"},
          {min: 6, message: "密码长度最少为6位", trigger: "blur"}
        ]
      }
    };
  },
  created() {

  },
  mounted() {
    this.loginUrl = loginUrlStore().getLoginUrl;
    this.tags = useTagList().getTag()
    this.beforeRoute = useRouterStore().getBeforeRoute()
    this.userInfo = useUserInfoStore().getUserInfo()
    this.novaToken = useTokenStore().getToken()

    if(this.novaToken!==null&&this.novaToken!=="null"){
      router.push("/nova/index");
    }
  },
  computed: {},
  props: [],
  methods: {
    encrypt(word) {
      const key = CryptoJS.enc.Utf8.parse('Dgf3f32iEDH6fWiV')
      const iv = CryptoJS.enc.Utf8.parse('Dgf3f32iEDH6fWiV')
      const srcs = CryptoJS.enc.Utf8.parse(word)
      const encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
      })
      return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          const param = {
            username: this.loginForm.username,
            grantType: 'pwd',
            tenantId: "1111",
            password: this.encrypt(this.loginForm.password)
          }
          new Promise((resolve, reject) => {
            loginByUsername(param).then((res) => {

              console.log('loginByUsername-res', res)
              resolve(res);
            }).catch(err => {
              console.log(err)
              reject(err)
            })
          }).then(res => {

            if (res.data.code === 200) {
              loginUrlStore().setLoginUrl("adminLogin")
              const authToken = res.data.data;
              useTokenStore().setToken(authToken.accessToken)
              router.push("/nova/index");
            }
          })


        }
      });
    }
  }
};
</script>

<style>
</style>
